package com.itboy.mall.pms.spuimages.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.pms.spuimages.vo.MallSpuImagesVo;
import com.itboy.mall.pms.spuimages.bo.MallSpuImagesQueryBo;
import com.itboy.mall.pms.spuimages.bo.MallSpuImagesAddBo;
import com.itboy.mall.pms.spuimages.bo.MallSpuImagesEditBo;
import com.itboy.mall.pms.spuimages.service.IMallSpuImagesService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * spu图片Controller
 *
 * @author itboy
 * @date 2021-08-08
 */
@Validated
@Api(value = "spu图片控制器", tags = {"spu图片管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/spuImages")
public class MallSpuImagesController extends BaseController {

    private final IMallSpuImagesService iMallSpuImagesService;

    /**
     * 查询spu图片列表
     */
    @ApiOperation("查询spu图片列表")
    @PreAuthorize("@ss.hasPermi('mall:spuImages:list')")
    @GetMapping("/list")
    public TableDataInfo<MallSpuImagesVo> list(@Validated MallSpuImagesQueryBo bo) {
        return iMallSpuImagesService.queryPageList(bo);
    }

    /**
     * 导出spu图片列表
     */
    @ApiOperation("导出spu图片列表")
    @PreAuthorize("@ss.hasPermi('mall:spuImages:export')")
    @Log(title = "spu图片", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallSpuImagesVo> export(@Validated MallSpuImagesQueryBo bo) {
        List<MallSpuImagesVo> list = iMallSpuImagesService.queryList(bo);
        ExcelUtil<MallSpuImagesVo> util = new ExcelUtil<MallSpuImagesVo>(MallSpuImagesVo.class);
        return util.exportExcel(list, "spu图片");
    }

    /**
     * 获取spu图片详细信息
     */
    @ApiOperation("获取spu图片详细信息")
    @PreAuthorize("@ss.hasPermi('mall:spuImages:query')")
    @GetMapping("/{id}")
    public AjaxResult<MallSpuImagesVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iMallSpuImagesService.queryById(id));
    }

    /**
     * 新增spu图片
     */
    @ApiOperation("新增spu图片")
    @PreAuthorize("@ss.hasPermi('mall:spuImages:add')")
    @Log(title = "spu图片", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallSpuImagesAddBo bo) {
        return toAjax(iMallSpuImagesService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改spu图片
     */
    @ApiOperation("修改spu图片")
    @PreAuthorize("@ss.hasPermi('mall:spuImages:edit')")
    @Log(title = "spu图片", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallSpuImagesEditBo bo) {
        return toAjax(iMallSpuImagesService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除spu图片
     */
    @ApiOperation("删除spu图片")
    @PreAuthorize("@ss.hasPermi('mall:spuImages:remove')")
    @Log(title = "spu图片" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMallSpuImagesService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
